<template>
  <div class="tags-container" style="margin-buttom:20px">
    <el-card>
      <!-- 筛选1 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="目录名称">
              <el-input
                style="200px"
                v-model="formData.directoryName"
              ></el-input>
            </el-form-item>

            <el-form-item label="状态">
              <el-select v-model="formData.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item style="text-align: right">
              <el-button @click="clearForm">清除</el-button>

              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="6" style="text-align: right">
          <el-button v-if="isShowBack" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
          <el-button @click="openDirectory('add')" icon="el-icon-edit" type="success" size="small">新增目录</el-button>
        </el-col>
      </el-row>

      <!-- 数据记录 -->

      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom: 15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->

      <el-table :data="directorysList">
        <el-table-column label="序号" type="index" width="80px"></el-table-column>
        <el-table-column label="所属学科" prop="subjectName"></el-table-column>
        <el-table-column label="目录名称" prop="directoryName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期">
          <template #default="{row}">
            {{formatDate(row.addDate)}}
          </template>
        </el-table-column>

        <el-table-column label="面试题数量" prop="totals"></el-table-column>
        <el-table-column label="状态" prop="state">
          <template #default="{row}">
            {{row.state === 1 ? '已启用' : '已禁用'}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template #default="{row}">
            <el-button type="text" @click="able(row)">
              {{row.state===1?'禁用':'启用'}}
            </el-button>
            <el-button type="text" @click="edit(row)" :disabled="row.state === 0">修改</el-button >
            <el-button type="text" @click="del(row)" :disabled="row.state === 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->

      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        hide-on-single-page
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="formData.pagesize"
        :current-page="formData.page"
        :page-sizes="[1,2,3,5,10]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>

      <!-- dialog -->
      <el-dialog @close="closeDialog"  width="30%" :title="title"  :visible="dialogVisible">
        <el-form ref="dialogFormRef" label-width="80px" :rules="rules" :model="dialogForm">
           <el-form-item v-if="!isShowBack" label="所属学科" prop="subjectID">
            <el-select style="width:100%" v-model="dialogForm.subjectID" placeholder="请选择">
              <el-option :label="item.label" :value="item.value" v-for="item in simpleSubjectList" :key="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="directoryName" label="目录名称">
            <el-input v-model="dialogForm.directoryName" placeholder="请输入目录名称"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list as reqDirList, add as reqAddDirectory, update as reqUpdateDirectory, remove as reqDeleteDirectory, changeState } from '@/api/hmmm/directorys'
import { simple as reqSimpleSubjectList } from '@/api/hmmm/subjects'
import { status as statusList } from '@/api/hmmm/constants'
export default {
  data () {
    return {
      str: '',
      statusList,
      dialogVisible: false,
      pagesize: 10,
      page: 1,
      total: 0,
      directorysList: [],
      TotalList: [],
      simpleSubjectList: [],
      formData: { directoryName: '', state: '' },
      dialogForm: { subjectID: '', directoryName: '' },
      rules: {
        subjectID: [
          { required: true, message: '请输入活动名称', trigger: ['blur', 'change'] }
        ],
        directoryName: [
          { required: true, message: '请选择活动区域', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.str === 'add' ? '新增目录' : '修改目录'
    },
    isShowBack () {
      console.log(this.$route)
      return !!this.$route.query.id
    },
    id () {
      return this.$route.query.id
    }
  },
  created () {
    this.getDirList()
  },
  methods: {
    clearForm () {
      this.formData = { directoryName: '', state: '' }
      // this.getDirList()
    },
    async search () {
      if (this.formData.directoryName || this.formData.state) {
        this.formData.page = 1
        const { data: { counts, items } } = await reqDirList({ ...this.formData, page: this.page, pagesize: this.pagesize })
        this.total = counts
        this.directorysList = items
      }
      if (!this.formData.directoryName && !this.formData.state) {
        this.getDirList()
      }
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getDirList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getDirList()
    },
    closeDialog () {
      this.dialogVisible = false
      this.dialogForm = { subjectID: '', directoryName: '' }
      this.$refs.dialogFormRef.resetFields()
    },
    // 处理时间
    formatDate (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 处理状态
    formatstatus (num) {
      return this.statusList.find(item => item.value === +num)?.label || '未知'
    },
    async getDirList () {
      // console.log(this.formData)
      const { data: { counts, items } } = await reqDirList({ page: this.page, pagesize: this.pagesize })
      this.total = counts
      this.directorysList = items
      // console.log(this.directorysList)
    },
    async openDirectory (str) {
      this.dialogVisible = true
      this.str = str
      const { data } = await reqSimpleSubjectList({})
      // console.log(data)
      this.simpleSubjectList = data
    },
    submit () {
      this.$refs.dialogFormRef.validate(async (boo) => {
        if (!boo) return this.$message.error('请填入完整信息')
        // console.log(this.dialogForm)
        if (this.str === 'add') {
          if (!this.id) {
            await reqAddDirectory(this.dialogForm)
          } else {
            this.dialogForm.subjectID = +this.id
            this.dialogForm.subjectName = this.subjectName
            await reqAddDirectory(this.dialogForm)
          }
          this.$message.success('新增目录成功')
        }
        if (this.str === 'edit') {
          await reqUpdateDirectory(this.dialogForm)
          this.$message.success('修改成功')
        }
        this.dialogVisible = false
        this.getDirList()
      })
    },
    edit (row, str) {
      // this.str = str
      // this.dialogVisible = true
      this.openDirectory('edit')
      this.dialogForm = row
    },
    del (row) {
      this.$confirm('此操作将删除该条目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await reqDeleteDirectory(row)
        this.$message.success('删除成功')
        this.getDirList()
      }).catch(() => {
      })
    },
    async able (row) {
      // console.log(row)
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState(row)
      this.$message.success('操作成功')
      // this.getDirList()
    }
  }
}
</script>

<style scoped lang='less'></style>
